package com.lp.article.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lp.article.service.IArticleService;
import com.lp.model.config.AppResult;
import com.lp.model.config.CodeMsg;
import com.lp.model.constants.RedisConstants;
import com.lp.model.dto.article.*;
import com.lp.model.entity.article.Article;
import com.lp.model.entity.article.ArticleStat;
import com.lp.model.entity.article.vo.ArticleESVO;
import com.lp.model.entity.article.vo.ArticleVO;
import com.lp.model.entity.article.vo.PageVO;
import com.lp.model.entity.user.User;
import com.lp.model.utils.PageUtils;
import com.lp.model.utils.UserUtils;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private IArticleService articleService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 最新列表
     * @param pageDto 分页信息
     */
    @PostMapping("/new_page")
    public AppResult newArticleList(@RequestBody @NonNull PageDto pageDto) {
        //1.校验分页信息是否合法
        if(!PageUtils.checkPageDto(pageDto)) {
            return AppResult.fail(CodeMsg.FAIL_ERROR_ARTICLE_PAGE);
        }
        //2.获取分页数据
        PageVO<ArticleVO> pageVO = articleService.getNewList(pageDto);
        return AppResult.success(pageVO);
    }

    /**
     * 发布文章
     * @param article 文章数据
     */
    @PostMapping("/publish")
    public AppResult publish(HttpServletRequest request, @RequestBody @NonNull Article article) {
        //1.非空校验
        if(article.getBoardName() == null ||
        !StringUtils.hasLength(article.getTitle()) || !StringUtils.hasLength(article.getDesc()) ||
        !StringUtils.hasLength(article.getContent())) {
            return AppResult.fail(CodeMsg.FAIL_NULL_ARTICLE);
        }
        //2.获取用户信息
        User userByToken = UserUtils.getUserByToken(redisTemplate, objectMapper, request);
        article.setUserId(userByToken.getId());
        //4.新增文章
        articleService.publish(article);
        return AppResult.success();
    }

    /**
     * 个人文章列表
     * @param pageDto 分页信息
     */
    @PostMapping("/self_list")
    public AppResult selfArticleList(@RequestBody @NonNull PageDto pageDto) {
        //1.校验分页信息是否合法
        if(!PageUtils.checkPageDto(pageDto)) {
            return AppResult.fail(CodeMsg.FAIL_ERROR_ARTICLE_PAGE);
        }
        //3.获取我的文章列表
        PageVO<ArticleVO> myList = articleService.getSelfList(pageDto);
        return AppResult.success(myList);
    }

    /**
     * 标记删除文章(普通用户不能真正删除文章)
     * @param id 文章 id
     */
    @GetMapping("/mark_del")
    public AppResult markDel(@NonNull Long id) {
        //1.数据库标记删除
        articleService.markDel(id);
        return AppResult.success();
    }


    /**
     * 修改文章信息
     * @param article 修改后的文章信息
     */
    @PostMapping("/update")
    public AppResult update(@RequestBody @NonNull Article article) {
        if(article.getBoardName() == null || article.getId() == null ||
                !StringUtils.hasLength(article.getTitle()) ||
                !StringUtils.hasLength(article.getDesc()) ||
                !StringUtils.hasLength(article.getContent())) {
            return AppResult.fail(CodeMsg.FAIL_NULL_ARTICLE);
        }
        articleService.update(article);
        return AppResult.success();
    }

    /**
     * 根据文章 id 获取文章信息
     * @param id 文章 id
     */
    @GetMapping("/info")
    public AppResult info(@NonNull Long id) {
        //1.获取文章数据
        ArticleVO articleVO = articleService.getInfo(id);
        return AppResult.success(articleVO);
    }

    /**
     * 获取用户统计数据
     * @param userId
     * @return
     */
    @GetMapping("/user_stat")
    public AppResult userStat(@NonNull Long userId) {
        return AppResult.success(articleService.getStatByUserId(userId));
    }

    /**
     * 为 es 远程调用提供
     * 获取文章 VO 信息(不增加文章访问量，不含统计信息)
     * @param id 文章id
     */
    @GetMapping("/es_vo")
    public AppResult ESVO(@NonNull Long id) {
        //1.获取文章数据
        ArticleESVO articleESVO = articleService.getArticleESVO(id);
        return AppResult.success(articleESVO);
    }

    @GetMapping("/get_article_stat")
    public AppResult getArticleStat(@NonNull Long id) {
        ArticleStat articleStat = articleService.getArticleStat(id);
        return AppResult.success(articleStat);
    }



}
